package com.quectel.cms.controller;

import com.google.common.collect.Maps;
import com.quectel.constant.global.UploadConstants;
import com.quectel.util.kit.StorageKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.FileUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("ueditor")
@Tag(name = "UeditorController", description = "富文本相关api")
public class UeditorController extends BaseController {


    @PostMapping(value = "/upload")
    @Operation(summary = "富文本上传")
    public Map upload(MultipartHttpServletRequest request) throws IOException {
        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                String originalFileName = file.getOriginalFilename();
                logger.info("上传文件名字为:{}", originalFileName);
                String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
                String fileName = file.getName();


                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (originalFileName.endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return resultMap("FAIL", null, 0, "不支持的文件格式", "", "");
                }

                long fileSize = file.getSize();
                String fileId = "ces";
                //产生一个临时文件
                File tempFile = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString() + "." + extension);

                try (InputStream in = file.getInputStream();
                     FileOutputStream fos = new FileOutputStream(tempFile)
                ) {
                    FileCopyUtils.copy(in, fos);

                    //上传
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);

                    return resultMap("SUCCESS", url, fileSize, fileId, fileName, extension);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return null;
    }

    private Map<String, Object> resultMap(String state, String url, long size, String title, String original, String type) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("state", state);
        result.put("original", original);
        result.put("size", size);
        result.put("title", title);
        result.put("type", type);
        result.put("url", url);
        return result;
    }
}
